package org.example.fboot.config.handler;

import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.example.fboot.handler.BusException;
import org.example.fboot.result.RequestStat;
import org.example.fboot.result.Result;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authorization.AuthorizationDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.reactive.resource.NoResourceFoundException;

import java.util.List;

/**
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public Result<String> processException(RuntimeException e) {
        log.error("RuntimeException:", e);
        return Result.result(RequestStat.run_error);
    }
    @ExceptionHandler(WebExchangeBindException.class)
    public Result<String> processWebExchangeBindException(WebExchangeBindException e){
        log.error("WebExchangeBindException:",e);
        FieldError error = e.getBindingResult().getFieldErrors().getFirst();
        return Result.msgResult(RequestStat.method_argument,"字段：["+error.getField()+"] "+error.getDefaultMessage());
    }
    @ExceptionHandler(AuthorizationDeniedException.class)
    public Result<String> processAuthorizationDeniedException(AuthorizationDeniedException e) {
        log.error("processAuthorizationDeniedException:", e);
        return Result.result(RequestStat.auth_invalid);
    }



    @ExceptionHandler(AuthenticationException.class)
    public Result<String> processAuthenticationException(AuthenticationException e) {
        log.error("processAuthenticationException:", e);
        return Result.msgResult(RequestStat.bus_error,e.getMessage());
    }
    @ExceptionHandler(AccessDeniedException.class)
    public Result<String> processAccessDeniedException(AccessDeniedException e) {
        log.error("processAccessDeniedException:", e);
        return Result.result(RequestStat.auth_invalid);
    }
    @ExceptionHandler(JwtException.class)
    public Result<String> processJwtException(JwtException e) {
        log.error("processJwtException:", e);
        return Result.result(RequestStat.token_invalid);
    }



    @ExceptionHandler(BadCredentialsException.class)
    public Result<String> processBadCredentialsException(BadCredentialsException e) {
        log.error("processBadCredentialsException:", e);
        return Result.result(RequestStat.user_password_error);
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoResourceFoundException.class)
    public Result<String> processNoResourceFoundException(NoResourceFoundException e) {
        log.error("processNoResourceFoundException:", e);
        return Result.result(RequestStat.not_found);
    }

    @ExceptionHandler(BusException.class)
    public Result<String> processBusException(BusException e) {
        log.error("BusException:", e);
        return Result.msgResult(RequestStat.bus_error, e.getMessage());
    }

    /**
     * 接口参数异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> processMethodArgumentException(MethodArgumentNotValidException e) {
        log.error("接口参数校验异常：MethodArgumentNotValidException:", e);
        return Result.msgResult(RequestStat.method_argument, e.getMessage());
    }

}
